package org.simplecrm.domain.event;

import org.hibernate.validator.constraints.NotBlank;
import org.simplecrm.domain.customer.Customer;
import org.simplecrm.domain.event.eventtype.EventType;
import org.simplecrm.domain.user.User;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.util.Date;

/**
 * Created by oler on 09.09.14.
 */
@Entity
@NamedQueries({
        @NamedQuery(name = "Event.count", query = "SELECT COUNT(e) FROM Event e WHERE e.customer.id = :customerId")
})
public class Event {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "description")
    @NotNull
    @NotBlank
    @Size(min = 2, max = 1024)
    @Pattern(regexp = "[^ ][ a-zA-Z0-9\"]+")
    private String description;

    @Column(name = "creationDate")
    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern = "yyyy/MM/dd")
    private Date creationDate;

    @Column(name = "status")
    @Enumerated(EnumType.STRING)
    @NotNull
    private EventStatus status;

    @ManyToOne(cascade = {CascadeType.REFRESH}, fetch = FetchType.LAZY)
    @JoinColumn(name = "customerId", referencedColumnName = "id", nullable = false)
    private Customer customer;

    @ManyToOne(cascade = {CascadeType.REFRESH})
    @JoinColumn(name = "typeId", referencedColumnName = "id", nullable = false)
    @NotNull
    private EventType type;

    @ManyToOne(cascade = {CascadeType.REFRESH})
    @JoinColumn(name = "userId", referencedColumnName = "id")
    private User support;

    public Event() {
    }

    public Event(String description, Date creationDate, EventStatus status, Customer customer, EventType type, User support) {
        this.description = description;
        this.creationDate = creationDate;
        this.status = status;
        this.customer = customer;
        this.type = type;
        this.support = support;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Transient
    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public EventStatus getStatus() {
        return status;
    }

    public void setStatus(EventStatus status) {
        this.status = status;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public EventType getType() {
        return type;
    }

    public void setType(EventType type) {
        this.type = type;
    }

    public User getSupport() {
        return support;
    }

    public void setSupport(User support) {
        this.support = support;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Event event = (Event) o;

        if (creationDate != null ? !creationDate.equals(event.creationDate) : event.creationDate != null) return false;
        if (description != null ? !description.equals(event.description) : event.description != null) return false;
        if (id != null ? !id.equals(event.id) : event.id != null) return false;
        if (status != null ? !status.equals(event.status) : event.status != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + (creationDate != null ? creationDate.hashCode() : 0);
        result = 31 * result + (status != null ? status.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Event{" +
                "id=" + id +
                ", description='" + description + '\'' +
                ", creationDate=" + creationDate +
                ", status='" + status + '\'' +
                ", customerName=" + (customer == null ? null : customer.getName()) +
                ", type=" + type +
                ", support=" + support +
                '}';
    }
}
